//
//  XHVisitorView.swift
//  gu-新浪微博
//
//  Created by xiaohui on 16/6/26.
//  Copyright © 2016年 gusheghui. All rights reserved.
//

import UIKit
import SnapKit
class XHVisitorView: UIView {

    //button按钮的点击 闭包
    var closure:(()->())?
    
    
    override init(frame: CGRect) {
        super.init(frame:frame )
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    
    private func setupUI(){
        
        backgroundColor = RandomColor()
        
        addSubview(feedImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLable)
        addSubview(loginButton)
        addSubview(registerButton)
        
        feedImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        messageLable.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(224)
            make.height.equalTo(36)
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(maskImageView)
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.left.equalTo(messageLable)
            make.top.equalTo(messageLable.snp_bottom).offset(16)
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.right.equalTo(messageLable)
            make.top.equalTo(loginButton)
        }
    }

    //设置中间图片滚动的动画
    func setupAnim(){
        let anim = CABasicAnimation(keyPath: "transform.rotaion")
        anim.duration = 10
        anim.toValue = M_PI * 2
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        feedImageView.layer.addAnimation(anim, forKey: nil)
        
    }
  
    private lazy var feedImageView:UIImageView = UIImageView(imgName: "visitordiscover_feed_image_smallicon")
    
    private lazy var maskImageView:UIImageView = UIImageView(imgName: "visitordiscover_feed_mask_smallicon")
    private lazy var iconImageView:UIImageView = UIImageView(imgName: "visitordiscover_feed_image_house")
    
    private lazy var messageLable:UILabel = {
       
        let lable = UILabel()
        lable.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lable.numberOfLines = 0
        lable.font = UIFont.systemFontOfSize(14)
        lable.textAlignment = .Center
        lable.textColor = ThemeColor
        return lable
    }()
    private lazy var loginButton:UIButton = {
       
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登陆", forState: UIControlState.Normal)
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        button.addTarget(self, action: "btnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    private lazy var registerButton:UIButton = {

        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)

        button.addTarget(self, action: "btnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    @objc private func btnClick(){
        closure?()
    }
    
    
    //首页和访客视图界面一样
    func setupVistorViewInfo(imageName:String?,title:String?){
        
        if let img = imageName,let title = title{
            
            iconImageView.image = UIImage(named: img)
            messageLable.text = title
            feedImageView.hidden = true
            
        }else{
            setupAnim()
        }
              
    }
    
    
    
}
